package com.mall.user.controller;

import com.central.common.model.Result;
import com.mall.entity.Role;
import com.mall.user.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;



    @PostMapping("/save")
    public Result save(@RequestBody Role role) {
        if ("".equals(role.getRoleName()) || role.getRoleName() == null) {
            return Result.failed("用户名为空");
        }
        List<Role> roleList = roleService.save(role);
        if (roleList != null) {
            return Result.succeed(roleList,"存储成功");
        }
        return Result.failed("存储失败");
    }


    @GetMapping("/all")
    public Result getAllUser() {
        List<Role> roleList = roleService.getAll();
        if (roleList != null) {
            return Result.succeed(roleList,"查询成功");
        }
        return Result.failed("无数据");
    }

    @DeleteMapping("/delete")
    public Result deleteRole(@RequestParam String roleId) {
        //删除角色
        List<Role> roleList = roleService.deleteRoleById(roleId);
        if (roleList != null) {
            return Result.succeed(roleList,"删除成功");
        }
        return Result.failed("删除失败");
    }

    @GetMapping("selectRoleByName")
    public Result getRoleByName(@RequestParam String roleName) {
        List<Role> roleList = roleService.selectRoleByName(roleName);
        if (roleList != null) {
            return Result.succeed(roleList,"查询成功");
        }
        return Result.failed("查询失败");
    }

}
